package com.fs.service.Impl;

import com.fs.constant.MessageConstant;
import com.fs.constant.StatusConstant;
import com.fs.dto.UserDTO;
import com.fs.dto.UserLoginDTO;
import com.fs.dto.UserRegisterDTO;
import com.fs.entity.User;
import com.fs.exception.PasswordErrorException;
import com.fs.mapper.UserMapper;
import com.fs.service.UserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDateTime;

@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    /*用户注册*/
    public String register(UserRegisterDTO registerDTO) {
        User user = new User();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(registerDTO.getPassword().getBytes());
        //拷贝用户属性
        BeanUtils.copyProperties(registerDTO,user);
        user.setPassword(md5DigestAsHex);
        //设置状态
        user.setStatus(1);
        //设置创建时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        Integer flag = userMapper.register(user);
        if (flag == 1) {
            return MessageConstant.REGISTER_SUCCESS;
        }else {
            return MessageConstant.REGISTER_ERROR;
        }

    }

    /*用户登录*/
    @SneakyThrows
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return user;
    }

    /*根据id查询用户信息*/
    public User getById(Long id) {
        //1.查询用户信息
        User user = userMapper.getById(id);
        //2.用户密码掩盖
        user.setId(null);
        user.setPassword(null);
        return user;
    }

    /*编辑用户信息资料*/
    public String update(UserDTO userDTO) {
        //1.拷贝用户信息
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        //2.更新
        user.setUpdateTime(LocalDateTime.now());
        Integer flag = userMapper.update(user);
        //3判断是否成功
        if (flag == 1) {
            return MessageConstant.UPDATE_SUCCESSFUL;
        }else {
            return MessageConstant.UPDATE_FAILED;
        }
    }


}
